package dataAcsessObject;

import entity.Course;
import java.util.List;
import javax.persistence.EntityManager;

/**
 *
 * @author Juan Hernández Serrato
 */
public class CourseDAO extends AbstractDataAccessObject<Course> {
    CourseDAO(){
        super(Course.class);
    }
    
    public List<Course> findCoursesByCategoryId(int id, EntityManager em){
        return em.createNamedQuery("CourseByCategory",Course.class)
                .setParameter("idc", id)
                .getResultList();
    }
    
    public List<Course> findCoursesByUniversity(String id, EntityManager em){
        return (List<Course>)em.createNativeQuery(
                "select * from COURSE where IDCOURSE in ( "
               +"select COURSE_HAS_PROFESSOR.COURSE_IDCOURSE from COURSE_HAS_PROFESSOR "
               +"inner join PROFESSOR "
               +"on "
               +"COURSE_HAS_PROFESSOR.PROFESSOR_PROFESSORUSER in "
               +"(select PROFESSORUSER from PROFESSOR where UNIVERSITY_USERUNIVERSITY = '"+id+"') "
               +"and "
               +"PROFESSOR.UNIVERSITY_USERUNIVERSITY = '"+id+"')", Course.class)
                .getResultList();
    }
    
    public List<Course> findCoursesByProfessor(String id, EntityManager em){
        return (List<Course>)em.createNativeQuery(
                "select c.* from COURSE c where c.idCourse in ("
               +"   select h.course_idCourse from COURSE_HAS_PROFESSOR h"
               +"        where h.course_idCourse = c.idCourse"
               +"           and"
               +" h.professor_professorUser = '"+id+"')",Course.class)
                .getResultList();
    }
}
